package com.ilop.sthome.ui.activity.config;

import android.content.Intent;
import android.os.Handler;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.example.common.utils.InputFilterUtil;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.common.command.SendDeviceCommand;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.ui.adapter.device.GatewayListAdapter;
import com.ilop.sthome.ui.adapter.room.RoomChooseAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.RoomDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityConfigDeviceBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2019-10-28.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：配置设备和房间信息
 */

public class ConfigDeviceActivity extends BaseBindActivity<ActivityConfigDeviceBinding> {

    private GatewayListAdapter mGatewayAdapter;
    private RoomChooseAdapter mRoomAdapter;
    private List<DeviceInfoBean> mGatewayList;
    private List<String> mNameList;
    private String mDeviceName;
    private String mDeviceType;
    private String mRoomId;
    private boolean isSubDevice;
    private int mPosition = 0;
    private Handler mHandler;
    private Runnable mRunnable;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_config_device;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mDeviceType = getIntent().getStringExtra(CommonId.KEY_DEVICE_TYPE);
        isSubDevice = getIntent().getBooleanExtra(CommonId.KEY_SUB_DEVICE, false);
    }

    @Override
    protected void initView() {
        super.initView();
        InputFilterUtil.setEditTextInhibitInputSpaChat(mContext, mDBind.etDeviceName);
        mGatewayAdapter = new GatewayListAdapter(mContext);
        mDBind.chooseGatewayList.setLayoutManager(new GridLayoutManager(mContext, 2));
        mDBind.chooseGatewayList.setAdapter(mGatewayAdapter);
        mHandler = new Handler();
    }

    @Override
    protected void initData() {
        super.initData();
        String deviceType = mContext.getString(SmartDevice.getType(mDeviceType).getTypeStrId());
        mDBind.etDeviceName.setHint(deviceType);
        getGatewayList();
        getRoomList();
        mRunnable = () -> {
            dismissProgressDialog();
            skipToAddSubDevice();
        };
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivGatewayBack.setOnClickListener(v -> finish());
        mDBind.ivAddGateway.setOnClickListener(view -> {
            skipAnotherActivity(ConfigGatewayActivity.class);
            finish();
        });
        mDBind.configDeviceNext.setOnClickListener(view -> {
            if (mPosition < 0) {
                showToast(getString(R.string.ali_rename_room_name));
            }else {
                List<RoomBean> mList = RoomDaoUtil.getInstance().findRoomNotEmptyAndDefault();
                mRoomId = mList.get(mPosition).getRid();
                onInsertDevice(mRoomId);
                showProgressDialog();
                mHandler.postDelayed(mRunnable,1000);
            }
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.CHOOSE_GATEWAY, Integer.class).observe(this, integer -> {
            mDeviceName = mGatewayList.get(integer).getDeviceName();
            mGatewayAdapter.setPosition(integer);
        });

        LiveDataBus.get().with(CommonId.CHOOSE_ROOM, Integer.class).observe(this , integer -> {
            this.mPosition = integer;
            mRoomAdapter.setPosition(integer);
        });
    }

    private void onInsertDevice(String roomId){
        DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        SendDeviceCommand mSendEquipment = new SendDeviceCommand(mContext, deviceInfoBean);
        mSendEquipment.increaseEquipment(roomId);
    }

    private void skipToAddSubDevice(){
        String nickName = mDBind.etDeviceName.getText().toString();
        Intent intent = new Intent(mContext, AddDeviceGuideActivity.class);
        intent.putExtra(CommonId.KEY_DEVICE_TYPE, mDeviceType);
        intent.putExtra(CommonId.KEY_DEVICE_NAME, mDeviceName);
        intent.putExtra(CommonId.KEY_SUB_DEVICE, isSubDevice);
        intent.putExtra(CommonId.KEY_DEVICE, nickName);
        intent.putExtra(CommonId.KEY_BIND_ROOM, mRoomId);
        startActivity(intent);
        finish();
    }

    private void getGatewayList(){
        if (isSubDevice){
            mGatewayList = new ArrayList<>();
            DeviceInfoBean mDevice = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
            mGatewayList.add(mDevice);
        }else {
            mGatewayList = DeviceDaoUtil.getInstance().findAllGatewayNotShare();
        }
        if (mGatewayList.size()>0){
            mGatewayAdapter.setList(mGatewayList);
            mGatewayAdapter.setPosition(0);
            mDeviceName = mGatewayList.get(0).getDeviceName();
        }else {
            mDBind.slHasGateway.setVisibility(View.GONE);
            mDBind.configDeviceNext.setVisibility(View.GONE);
            mDBind.llNoGateway.setVisibility(View.VISIBLE);
        }
    }

    private void getRoomList(){
        mNameList = RoomDaoUtil.getInstance().findRoomName(mContext);
        GridLayoutManager mManager = new GridLayoutManager(mContext, 4);
        mManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                return ConfigureUtil.gridLayoutLineNum(mNameList.get(i).length());
            }
        });
        mRoomAdapter = new RoomChooseAdapter(mContext, mNameList);
        mDBind.chooseRoomList.setLayoutManager(mManager);
        mDBind.chooseRoomList.setAdapter(mRoomAdapter);
        mRoomAdapter.setPosition(mPosition);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler!=null) {
            mHandler.removeCallbacks(mRunnable);
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
